
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ivy_ezviz_plugin/control/EzvizUIKitControl.dart';

import '../ivy_ezviz_plugin.dart';

///
class EzvizUIKitView extends StatefulWidget {
  ///
  String AppKey = "";
  String accessToken = "";
  String playUrl = "";
  EventCallback callback;
  EzvizUIKitViewCreatedCallback ezvizUIKitViewCreatedCallback;

  EzvizUIKitView(
      {this.accessToken,
        this.AppKey,
        this.playUrl,
        this.callback,
        this.ezvizUIKitViewCreatedCallback});

  @override
  _EzvizUIKitViewState createState() => _EzvizUIKitViewState();
}

class _EzvizUIKitViewState extends State<EzvizUIKitView> {
  @override
  Widget build(BuildContext context) {
    if (defaultTargetPlatform == TargetPlatform.android) {
      return AndroidView(
        onPlatformViewCreated: _onPlatformViewCreated,
        viewType: "com.ivy.ezvizUIKit.view",
        creationParams: <String, dynamic>{
          "AppKey": widget.AppKey.toString(),
          "accessToken": widget.accessToken.toString(),
          "playUrl": widget.playUrl.toString(),
        },
        creationParamsCodec: const StandardMessageCodec(),
      );
    } else if (defaultTargetPlatform == TargetPlatform.iOS) {
      return UiKitView(
        viewType: "com.ivy.ezvizUIKit.view",
        onPlatformViewCreated: _onPlatformViewCreated,
        creationParams: <String, dynamic>{
          "AppKey": widget.AppKey.toString(),
          "accessToken": widget.accessToken.toString(),
          "playUrl": widget.playUrl.toString(),
        },
        creationParamsCodec: const StandardMessageCodec(),
      );
    } else {
      print("flutter_plugin_demo 不支持当前平台");
      return Container();
    }
  }

  void _onPlatformViewCreated(int id) {
    EzvizUIKitControl controller = EzvizUIKitControl(id, widget.callback);
    widget.ezvizUIKitViewCreatedCallback(controller);
  }
}
